package com.yiya.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yiya.bean.News;
import com.yiya.bean.SysUser;
import com.yiya.mapper.NewsMapper;
import com.yiya.model.NewsModel;

@Service("newsService")
public class NewsService{
	@Autowired
	private NewsMapper newsMapper;

	public List<News> queryByList(NewsModel bean) {
		Integer rowCount = queryByCount(bean);
		bean.getPager().setRowCount(rowCount);
		List<News> news = newsMapper.selectByModel(bean);
		return news;
	}

	public int queryByCount(NewsModel bean) {
		return newsMapper.countByModel(bean);
	}
	
	public int add(News news){
		return newsMapper.insert(news);
	}
	
	public int Update(News news){
		return newsMapper.updateByPrimaryKeySelective(news);
	}
	
	public News queryById(Integer id){
		return newsMapper.selectByPrimaryKey(id);
	}
	
	public void delete(Integer[] ids,SysUser user){
		for(Integer id:ids){
			Date nowTime = new Date();
			News news = newsMapper.selectByPrimaryKey(id);
			news.setUpdateBy(user.getId());
			news.setUpdateTime(nowTime);
			news.setDelFlag(1);
			newsMapper.updateByPrimaryKeySelective(news);
		}
	}
	
	public List<News> getIndexNews(Map<String, Object> param){
		param.put("startIndex", 0);
		param.put("count", 12);
		return newsMapper.selectIndexNews(param);
	}
	
	
}
